/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * taskinfo.java
 *
 * Created on 14-jun-2011, 22:38:32
 */
package todotask.view;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import todotask.Adapters.DeleteTask;
import todotask.Adapters.ViewPropertiesMouseListener;
import todotask.BackgroundPanel;
import todotask.core.StopWatch;
import todotask.entities.Category;
import todotask.entities.Task;

/**
 *
 * @author dakota
 */
public class TaskInfo extends javax.swing.JPanel implements PropertyChangeListener {

    private Task task;
    private StopWatch crono;

    public TaskInfo(Task t) {
        task = t;
        crono = new StopWatch();
        initComponents();
        setTask(task);
    }

    public final void setTask(Task t) {
        task = t;
        setTitle(t.getTitle());
        setDescription(t.getDescription());
        setImportant(t.getStatus().getStatus());
        setColorTask(t.getCategory());
        if (crono == null) {
            crono = new StopWatch();
        }
        crono.setElapseTimeSecs(t.getTime());
        crono.addPropertyChangeListener(this);
        task_time.setText(crono.getElapsedStringTime());
        setTimestamp(crono.getElapsedStringTime());
        play.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent ce) {
                if (ce.getSource() instanceof JCheckBox) {
                    JCheckBox combo = (JCheckBox) ce.getSource();
                    System.out.println("Name CheckBox: " + combo.getText());
                    if (combo.isSelected()) {
                        crono.start();
                    } else {
                        crono.stop();
                        task.setTime(WIDTH);
                    }
                }
            }
        });
        finished.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent ie) {
                if (finished.isSelected()) {
                    task.setFinishDate(Calendar.getInstance());
                } else {
                    //
                }
            }
        });

        view_properties.addMouseListener(new ViewPropertiesMouseListener(null, getT()));
        deleteTask.addMouseListener(new DeleteTask());
    }

    private void setColorTask(Category cat) {
        jPanel1.setBackground(cat.getColor());
    }

    private void setImportant(String status) {
        if (status.equals("En Curso")) {
            task_important.setSelected(true);
        } else {
            task_important.setSelected(false);
        }
    }

    private void setTimestamp(String time) {
        task_time.setText(time);
    }

    private void setDescription(String description) {
        task_description.setText(description);
    }

    private void setTitle(String title) {
        task_title.setText(title);
    }

    /** Creates new form taskinfo */
    public TaskInfo() {
        initComponents();
    }

    public Task getT() {
        return task;
    }

    public void setT(Task t) {
        this.task = t;

    }

//    @Override
//    public String toString() {
//        return task.toString();
//    }
    public BackgroundPanel getJPanel() {
        return (BackgroundPanel) jPanel1;
    }

    /**
     * Con este metodo debemos actualizar el tiempo transcurrido
     * @param pce 
     */
    @Override
    public void propertyChange(PropertyChangeEvent pce) {
        if (pce.getSource() instanceof StopWatch) {
            task_time.setText(pce.getNewValue().toString());
            getT().setTime(Integer.parseInt(pce.getOldValue().toString()));
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new BackgroundPanel(false);
        task_title = new javax.swing.JLabel();
        task_description = new javax.swing.JLabel();
        task_time = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        task_important = new javax.swing.JCheckBox();
        finished = new javax.swing.JCheckBox();
        play = new javax.swing.JCheckBox();
        view_properties = new javax.swing.JButton();
        deleteTask = new javax.swing.JButton();

        task_title.setText("Title");

        task_description.setText("Description");

        task_time.setText("Timestamp");

        jPanel2.setOpaque(false);

        task_important.setIcon(new javax.swing.ImageIcon(getClass().getResource("/todotask/images/flag_gray.png"))); // NOI18N
        task_important.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/todotask/images/flag.png"))); // NOI18N

        finished.setFocusPainted(false);

        play.setIcon(new javax.swing.ImageIcon(getClass().getResource("/todotask/images/start_gray.png"))); // NOI18N
        play.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/todotask/images/start.png"))); // NOI18N

        view_properties.setIcon(new javax.swing.ImageIcon(getClass().getResource("/todotask/images/property_blue.png"))); // NOI18N
        view_properties.setBorderPainted(false);
        view_properties.setFocusPainted(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(finished, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
            .addComponent(play)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(view_properties, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(task_important, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(finished, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(play)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(view_properties)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(task_important)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        deleteTask.setIcon(new javax.swing.ImageIcon(getClass().getResource("/todotask/images/1310151148_fileclose.png"))); // NOI18N
        deleteTask.setBorderPainted(false);
        deleteTask.setFocusPainted(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(task_title, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(task_description, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                        .addComponent(task_time, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(deleteTask))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(task_title)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(task_description, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(task_time)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteTask, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(248, 248, 248))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteTask;
    private javax.swing.JCheckBox finished;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JCheckBox play;
    private javax.swing.JLabel task_description;
    private javax.swing.JCheckBox task_important;
    private javax.swing.JLabel task_time;
    private javax.swing.JLabel task_title;
    private javax.swing.JButton view_properties;
    // End of variables declaration//GEN-END:variables
}